// pages/menu/menu.js
let soucang = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    alist: [],
    yongliao: [],
    cainame: '',
    isSouCang: false,
    index: null,
    clickcount: 0,
    id: 0
  },
  //收藏
  clickMe(e) {

    //提示用户
wx.showLoading()
    if (!this.data.isSouCang) {
      console.log(this.data.alist);
      console.log(this.data.alist.did);
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/collect?caipu_id=' + this.data.alist.did + "&" + "avator=" + this.data.alist.thumb + '&' + 'title=' + this.data.alist.title + '&' + 'brief=' + this.data.alist.steptext,
        method: 'GET',
        header: {
          'content-type': 'application/json', // 默认值
          'sessiontoken': wx.getStorageSync('token')
        },
        success: (res) => {
          console.log(res);
          let isSouCang = !this.data.isSouCang
          this.setData({
            isSouCang,
            id:res.data.data.insertId
          })
          wx.showToast({
            title: '收藏成功',
            icon: 'success'
          })
        }
      })
    } else {
      console.log(this.data.id);
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/deleteinfo?id=' + this.data.id,

        success: res => {
          console.log(res.data);
          if (res.data.delstate != 0) {
            let isSouCang = !this.data.isSouCang
            this.setData({
              isSouCang
            })
          }
          wx.showToast({
            title: res.data.delstate == 0 ? '取消失败' : '取消成功',
            icon: res.data.delstate == 0 ? 'error' : 'success'
          })
        },
        fail: err => {
          console.log(err);
          wx.showToast({
            title: '出错了...',
          })
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.info);

    var that = this
    var info = null;
    if (options.info == undefined) {
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/classify?name=' + options.name,
        success: res => {
          console.log(res.data.data);
          info = res.data.data.data[0]
          that.parseInfo(info)
        }
      })
      return
    }
    info = JSON.parse(decodeURIComponent(options.info));
    this.setData({
      id: info.id
    })
    this.parseInfo(info)

  },
  parseInfo: function (info) {
    console.log(info);
    if (info.yl != null) {
      var ylArr = info.yl.replace(/↵/g, '\n').split('\n')  //正则全局替换
      // var yl = ""
      // ylArr.forEach(ele => {
      //   yl += ele
      // })
      console.log(ylArr);
      this.setData({
        yongliao: ylArr
        
      })

    }
    if (info.fl != null) {
      var flArr = info.fl.replace(/↵/g, '\n').split('\n') 
      // var fl = ""
      // flArr.forEach(ele => {
      //   fl += ele
      // })
      this.setData({
        xiangqing: flArr,
      })
    }



    console.log(info.steptext);
    if (info.steptext != null) {
      var fStep = info.steptext.split("#")
      console.log(fStep);
      this.setData({
        step: fStep,
      })
    }
    this.setData({
      alist: info,
      isSouCang: info.isC,

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})